export{}
function createArray<T> (length:number,value:any):Array<T>{
	let result:any[] = []
	for(let i=0;i<length;i++){
		result[i] = value
	}
	return result
}
let result = createArray<string>(3,'x')

function sum() {
	let args: IArguments = arguments;
	for (let i = 0; i < args.length; i++) {
			console.log(args[i]);
	}
}
sum(1, 2, 3);

let root = document.getElementById('root');
let children: HTMLCollection = (root as HTMLElement).children;
children.length;
let nodeList: NodeList = (root as HTMLElement).childNodes;
nodeList.length;


// 泛型类
class MyArray<T>{
	private list:T[] = []
	add(value:T){
		this.list.push(value)
	}
	getMax():T{
		return this.list[0]
	}
}
let array = new MyArray<number>();
array.add(1)
array.add(2)
array.add(3)
console.log(array.getMax())


// 泛型与new
function factory<T>(type:{new():T}):T{
	return new type()
}
class Person{}
let p =factory<Person>(Person)
console.log(p)

// 泛型接口
interface Calculate{
	<T>(a:T,b:T):T
}
let sum2:Calculate = function<T>(a:T,b:T){
	return a;
}
sum2<number>(1,2)


interface Calculate2<T>{
	(a:T,b:T):T
}
let sum3:Calculate2<number> = function(a:number,b:number):number{
	return a+b;
}
sum3(1,2)

interface Calculate3<T>{
	<U>(a:T,b:T):U
}
let sum4:Calculate3<number> = function<U>(a:number,b:number):U{
	return a as any;
}
sum4<number>(1,2);

//泛型可以写多个
function swap<A,B> (tuple:[A,B]):[B,A]{
	return [tuple[1],tuple[0]]
}

//默认泛型
function createArray3<T=number>(length: number, value: T): Array<T> {
  let result: T[] = [];
  for (let i = 0; i < length; i++) {
    result[i] = value;
  }
  return result;
}
let result2 = createArray3(3,'x');
console.log(result2);
interface T2<T=string>{

}
type T22 = T2<string>


// 泛型约束
function logger<T>(val:T){
	console.log(val.length)
}
interface LengthWise{
	length:number
}
function logger2<T extends LengthWise>(val:T){
	console.log(val.length)
}

let obj = {
	length:10
}
type Obj = typeof obj;
logger2<Obj>(obj)


interface Calculate{
	<T extends (string | number)> (a:T,b:T):void
}
let summ:Calculate = function <T extends (string | number)>(a:T,b:T):void{
	
}
summ<(string | number)>(1,2)

interface Calculatesum{
	<T extends (string | number)> (a:T,b:T):void
}
let summ2:Calculatesum = function <T extends (string | number)>(a:T,b:T):void{

}
/**
 * string 是(string | number)的子类型
 * (string | number)是 (string | number |boolean)的子类型
 */
summ2<string>('1','2')